<?php
class IndexController extends Zend_Controller_Action {
	public function init() {
		/* Initialize action controller here */
	}
	public function indexAction() {
		$pageNum = $this->getParam ( "page" );
		
		// get banners
		$bannerDb = new Application_Model_DbTable_Banner ();
		$where = $bannerDb->getAdapter ()->quoteInto ( "del_flag = ?", 0 );
		$banners = $bannerDb->fetchAll ( $where );
		
		// get all phones's data
		$phoneDb = new Application_Model_DbTable_Phone ();
		$phones = $phoneDb->fetchAll ( $where );
		
		$paginator = Zend_Paginator::factory ( $phones );
		$paginator->setDefaultItemCountPerPage ( 6 );
		
		if ($pageNum != null) {
			$paginator->setCurrentPageNumber ( $pageNum );
		} else {
			$paginator->setCurrentPageNumber ( 1 );
		}
		
		// get all catagories
		$catagoryDb = new Application_Model_DbTable_Catagory ();
		$catagories = $catagoryDb->fetchAll ( $where );
		
		$this->view->banners = $banners;
		$this->view->paginator = $paginator;
		$this->view->catagories = $catagories;
	}
	public function catagoryAction() {
		// get parameters
		$pageNum = $this->getParam ( "page" );
		$catagoryId = $this->getParam ( "id" );
		
		// get banners
		$bannerDb = new Application_Model_DbTable_Banner ();
		$where = $bannerDb->getAdapter ()->quoteInto ( "del_flag = ?", 0 );
		$banners = $bannerDb->fetchAll ( $where );
		
		// get all phones's data
		$phoneDb = new Application_Model_DbTable_Phone ();
		$wherePhone = $phoneDb->getAdapter ()->quoteInto ( "catagory_id=? AND del_flag = 0", $catagoryId );
		$phones = $phoneDb->fetchAll ( $wherePhone );
		
		$paginator = Zend_Paginator::factory ( $phones );
		$paginator->setDefaultItemCountPerPage ( 6 );
		
		if ($pageNum != null) {
			$paginator->setCurrentPageNumber ( $pageNum );
		} else {
			$paginator->setCurrentPageNumber ( 1 );
		}
		
		// get all catagories
		$catagoryDb = new Application_Model_DbTable_Catagory ();
		$catagories = $catagoryDb->fetchAll ( $where );
		
		// get current catagory
		$whereCatagory = $catagoryDb->getAdapter()->quoteInto("id=?", $catagoryId);
		$currentCatagory = $catagoryDb->fetchRow($whereCatagory);
		
		$this->view->banners = $banners;
		$this->view->paginator = $paginator;
		$this->view->catagories = $catagories;
		$this->view->currentCatagory = $currentCatagory;
	}
}

